package com.czl.module_service.viewmodel

import android.os.Bundle
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.UserInfo
import com.czl.base.data.bean.tengyun.StorehouseBean
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class ServiceViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val btnWorkClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Work.F_WORK)
    })

    val btnReportClick: BindingCommand<Void> = BindingCommand(BindingAction {
        showNormalToast("开发中...")
    })
    val onNavMyScoreClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.User.F_MY_SCORE)
    })
    val btnActivityClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Activities.F_ACTIVITIES_LIST)
    })
    val btnBullentinClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Activities.F_ACTIVITIES_BULLETIN_LIST)
    })
    val btnCarAcessClick: BindingCommand<Void> = BindingCommand(BindingAction {

        startContainerActivity(AppConstants.Router.Park.F_CAR_ACCESS)
    })

    val btnAssetHandleClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Service.F_SERVICE_ASSET_HANDLE_APPLY)
    })

    val btnWorkOrderCenterClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Work.F_WORK_MY_ORDER)
    })
    val btnComplaintManagerClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Complaint.F_COMPLAINT_MANAGER)
    })

    val btnDeviceTakeCareClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Work.F_WORK_DEVICE_TAKE_CARE)
    })

    val btnDevicePatrolClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Work.F_WORK_DEVICE_PATROL)
//        startContainerActivity(AppConstants.Router.Work.F_WORK_DEVICE_INSPECTION)
    })

    val btnElectronClick: BindingCommand<Void> = BindingCommand(BindingAction {
//        startContainerActivity(AppConstants.Router.Report.F_REPORT_SUBMIT)
    })

    val btnStoreManageClick: BindingCommand<Void> = BindingCommand(BindingAction {
        getStorehouseByUserId(type = 2)
    })

    //    采购
    val btnPurchaseClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Service.F_SERVICE_PURCHASE_APPLY)
    })

    //    领用
    val btnUsageClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Service.F_SERVICE_ASSET_USAGE, Bundle().apply {
            putSerializable(AppConstants.BundleKey.USAGE_APPLICATION_TYPE, 0)
        })
    })

    //    借用
    val btnBorrowClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Service.F_SERVICE_ASSET_USAGE, Bundle().apply {
            putSerializable(AppConstants.BundleKey.USAGE_APPLICATION_TYPE, 1)
        })
    })

    //    调拨
    val btnWareClick: BindingCommand<Void> = BindingCommand(BindingAction {
        getStorehouseByUserId(type = 1)
    })

    // 盘点
    val btnInventoryClick: BindingCommand<Void> = BindingCommand(BindingAction {
        startContainerActivity(AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY_HOME)
    })

    /**
     * 获取用户详细信息
     */
    fun getUserInfo() {
        model.apply {
            getUserInfoNet()
                .compose(RxThreadHelper.rxSchedulerHelper(this@ServiceViewModel))
                .subscribe(object : ApiSubscriberHelper<BaseBean<UserInfo>>() {
                    override fun onResult(t: BaseBean<UserInfo>) {
                        t.data?.let {
                            saveUserInfoData(it)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showNormalToast(msg)
                    }

                })
        }
    }

    private fun getStorehouseByUserId(
        type: Int? = null
    ) {
        model.mTengYunHttpDataSource.getStorehouseByUserId(
            GsonUtils.toJson(
                mapOf(
                    "userId" to model.getLocalUserInfo()?.userId,
                    "companyId" to model.getLoginCompanyId()?.toInt()
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<StorehouseBean>>() {
                override fun onResult(t: BaseBean<StorehouseBean>) {
                    if (t.state == 0) {
                        if(t.data?.list?.size!! > 0) {
                            if (type == 2) {
                                startContainerActivity(AppConstants.Router.Service.F_SERVICE_STORE_MANAGE)
                            }
                            if (type == 1) {
                                if (t.data?.list?.size!! == 1) {
                                    showNormalToast("用户只有一个仓库，无需调拨操作！")
                                } else {
                                    startContainerActivity(AppConstants.Router.Service.F_SERVICE_ASSET_STORE)
                                }
                            }
                        } else {
                            if (type == 1) {
                                showNormalToast("非仓库管理员，无调拨操作权限！")
                            } else {
                                showNormalToast("非仓库管理员，无仓库管理操作权限！")
                            }
                        }
                    } else {
                        showErrorToast(t.msg)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}